#include "permutation_combination_api.h"
#include "combination.h"
#include "base_math.h"
#include <math.h>


/// <summary>
/// cs the specified m.
/// </summary>
/// <param name="m">The m.</param>
/// <param name="n">The n.</param>
/// <returns></returns>
double C(int m, int n) {
	return (double)fact(m) / (double)((fact(m - n)*fact(n)));
}


uint32_t Ci(int m, int n, int index, uint8_t *buff) {
	int loop = 0;
	double tmp;

	if (0 == n) return -1;

	if (m == n) {
		for (loop = 0; loop < m; loop++) {
			buff[loop] = loop;
		}
		return 0;
	}

	if (2 == m) {
		uint8_t combination[2] = {0,1};
		buff[0] = combination[index];
	}

	tmp = C(m - 1, n);

	if (index <tmp)
	{
		Ci(m - 1, n, index, buff);
	}else{
		buff[n-1] = (uint8_t)m - 1;
		Ci(m - 1, n - 1, (int)ceil(index - tmp), buff);
	}
	return 0;
}
